package servlet;

import util.*;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class UserLogin extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取客户端请求的参数，并进行判断。

        String name= "";//取得用户名参数
        String pass="";//取得用户密码参数
        try {
            name=request.getParameter("name").trim();//取得用户名参数
            pass=request.getParameter("pass").trim();//取得用户密码参数

        }catch (Exception e){

        }

        User Login=new User();
        Login.setName(name);//为JavaBean设置name属性
        Login.setPass(pass);// 为JavaBean设置pass属性



        try{
            if(Login.check()){  //如果用户存在


                HttpSession session = request.getSession();
                session.setAttribute("user", name);


                RequestDispatcher dispatcher=
                        request.getRequestDispatcher("index.jsp");//转发
                dispatcher.forward(request,response);
            }
            else {

                RequestDispatcher dispatcher=
                        request.getRequestDispatcher("login.jsp");//转发
                dispatcher.forward(request,response);

//                request.setAttribute("msg","登录失败");
//                request.getRequestDispatcher("login.jsp");
            }
        }catch(Exception e){


        }


//
//        RequestDispatcher dispatcher=
//                request.getRequestDispatcher("login.jsp");//转发
//        dispatcher.forward(request,response);

    }
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }
}